package com.sbox.album;

import com.sbox.album.model.AlbumContext;
import com.sbox.album.model.AlbumInfo;
import com.sbox.util.AlbumConst;
import com.sbox.util.AlbumLog;
import com.sbox.widget.PicGridView;
import com.sbox.widget.TitleBar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

public class AlbumActivity extends Activity {
	private TitleBar mTitleBar = null;
	private AlbumInfo mAlbum = null;
	private PicGridView mGridView = null;	
	
	private View.OnClickListener mAddPicListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {
			Intent i = new Intent(AlbumActivity.this, ImportActivity.class);
			AlbumActivity.this.startActivityForResult(i, AlbumConst.REQUEST_IMPORT_PICTURE);
		}
	};
	
	private AdapterView.OnItemClickListener mGridClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {	
			AlbumContext.setPicture( mAlbum.getPicture(position) );
			AlbumContext.setPicIndex(position);
			
			Intent i = new Intent(AlbumActivity.this, PictureActivity.class);
			AlbumActivity.this.startActivityForResult(i, AlbumConst.REQUEST_VIEW_PICTURE);
		}		
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_album);
     
        mAlbum = AlbumContext.getAlbum();
        if( mAlbum == null )
        	return;
        
        mTitleBar = (TitleBar)findViewById(R.id.album_title_bar);
        mTitleBar.getButton2().setImageResource(R.drawable.btn_add_pic);
        mTitleBar.getButton2().setOnClickListener(mAddPicListener);
        
        mGridView = (PicGridView)findViewById(R.id.album_grid);
        mGridView.setOnItemClickListener(mGridClickListener);
    }
	
	@Override
	public void onResume() {
		super.onResume();
		
		AlbumLog.info("AlbumActivity.onResume");
		if( mAlbum == null )
			return;
		
		AlbumContext.getCacheManager().clearCache();
		
		mTitleBar.getTextView().setText(mAlbum.getName());
		mGridView.loadImages(mAlbum.getPath());
	}	
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
		AlbumLog.info("AlbumActivity.onActivityResult");
		
		mAlbum.update();
    }
}
